﻿using System;
using System.Reflection;
using Sunny.UI;

namespace AutoShuntingSchedule.UI.Forms
{
    public partial class FPieChart : UIPage
    {
        public FPieChart()
        {
            InitializeComponent();
            this.uiSplitContainer1.Panel2Collapsed = true;
            uiSymbolButton1_Click(null, null);
        }

        private void uiImageButton1_Click(object sender, System.EventArgs e)
        {
            PieChart.ChartStyleType = UIChartStyleType.Default;
        }

        private void uiImageButton2_Click(object sender, System.EventArgs e)
        {
            PieChart.ChartStyleType = UIChartStyleType.Plain;
        }

        private void uiImageButton3_Click(object sender, System.EventArgs e)
        {
            PieChart.ChartStyleType = UIChartStyleType.Dark;
        }

        private void uiSymbolButton1_Click(object sender, System.EventArgs e)
        {
            var option = new UIPieOption();

            //设置Title
            option.Title = new UITitle();
            option.Title.Text = "类库统计";
            option.Title.SubText = "数量";
            option.Title.Left = UILeftAlignment.Center;

            //设置ToolTip
            option.ToolTip.Visible = true;

            //设置Legend
            option.Legend = new UILegend();
            option.Legend.Orient = UIOrient.Vertical;
            option.Legend.Top = UITopAlignment.Top;
            option.Legend.Left = UILeftAlignment.Left;
            //var cnt = 7;
            //var dateTime = DateTime.Now;
            //for (int i = 0; i < cnt; i++)
            //{
            //    option.Legend.AddData($"{dateTime.Year}-{dateTime.Month}-{dateTime.Day - cnt + i}");
            //}
            //option.Legend.AddData("2020-05-19");
            //option.Legend.AddData("2020-05-20");
            //option.Legend.AddData("2020-05-21");
            //option.Legend.AddData("2020-05-22");
            //option.Legend.AddData("2020-05-23");
            //option.Legend.AddData("2020-05-24");
            //option.Legend.AddData("2020-05-25");
            // 获取当前进程的所有加载的程序集
            

            //设置Series
            var series = new UIPieSeries();
            series.Name = "StarCount";
            series.Center = new UICenter(50, 55);
            series.Radius = 70;
            series.Label.Show = true;

            //增加数据
            //series.AddData("2020-05-19", 38);
            //series.AddData("2020-05-20", 21);
            //series.AddData("2020-05-21", 11);
            //series.AddData("2020-05-22", 52);
            //series.AddData("2020-05-23", 23);
            //series.AddData("2020-05-24", 26);
            //series.AddData("2020-05-25", 27);
            //var r = new Random(cnt);
            //for (int i = 0; i < cnt; i++)
            //{
            //    series.AddData($"{dateTime.Year}-{dateTime.Month}-{dateTime.Day - cnt + i}", r.Next(cnt));
            //}
            Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();

            // 遍历并打印程序集信息
            foreach (var assembly in assemblies)
            {
                try
                {

                    Type[] types = assembly.GetTypes();
                    var data = $"{assembly.FullName}";
                    data = data.Substring(0, data.IndexOf(",", StringComparison.Ordinal));
                    if (!assembly.GlobalAssemblyCache && types.Length < 200 && !data.Contains("shost"))
                    {
                        option.Legend.AddData(data);
                        // 打印程序集中的所有类型
                        series.AddData(data, types.Length);
                    }
                    
                }
                catch (Exception exception)
                {
                    Console.WriteLine(exception.Message);
                }
            }


            //增加Series
            option.Series.Clear();
            option.Series.Add(series);

            //显示数据小数位数
            option.DecimalPlaces = 0;

            //设置Option
            PieChart.SetOption(option);

            uiSymbolButton2.Enabled = true;
        }

        private void uiSymbolButton2_Click(object sender, System.EventArgs e)
        {
            Random random = new Random(DateTime.Now.Millisecond);
            PieChart.Update("StarCount", "2020-05-19", random.Next(50));
            PieChart.Update("StarCount", "2020-05-20", random.Next(50));
            PieChart.Update("StarCount", "2020-05-21", random.Next(50));
            PieChart.Update("StarCount", "2020-05-22", random.Next(50));
            PieChart.Update("StarCount", "2020-05-23", random.Next(50));
            PieChart.Update("StarCount", "2020-05-24", random.Next(50));
            PieChart.Update("StarCount", "2020-05-25", random.Next(50));

            PieChart.Refresh();
        }
    }
}